//
//  ThreeArtTableViewController.m
//  开心一刻
//
//  Created by lanou on 16/4/7.
//  Copyright © 2016年 刘洋. All rights reserved.
//

#import "ThreeArtTableViewController.h"

#import "Requestmanager.h"
#import "HZWPictureModel.h"
#import "HZWPictureTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "HZWWebViewController.h"
#import "MJRefresh.h"


@interface ThreeArtTableViewController ()
@property(nonatomic,strong)NSMutableArray * modelArray;
@property (nonatomic,strong)Requestmanager * manage;
@property(nonatomic,assign)NSInteger aa;
@end

@implementation ThreeArtTableViewController
// 上拉加载
-(void)requestFresh{
    self.aa +=20;
    NSString * string = @"http://s.budejie.com/topic/list/jingxuan/29/bs0315-iphone-4.1/";
    NSString * str = [NSString stringWithFormat:@"0-%ld.json",self.aa];
    NSString * str1 =[string stringByAppendingString:str];
    [Requestmanager requestWithUrl:str1 requesType:requestTypeGET parDic:nil finish:^(NSData *data) {
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
                            
            NSMutableArray * array = [HZWPictureModel creatPictureModel:dic];
            for (int i = 0; i < array.count; i++)
                {
                 if(i > _modelArray.count)
                    {
                    [_modelArray addObject:array[i]];
                                    
                    }
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.tableView reloadData];
                });
                [self.tableView .mj_footer endRefreshing];
                            
                } error:^(NSError *error) {
                    NSLog(@"请求失败");
        }];

    
}

// 进行网络请求
-(void)craetPicture{
    
    [Requestmanager requestWithUrl:@"http://s.budejie.com/topic/list/jingxuan/29/bs0315-iphone-4.1/0-40.json"
        requesType:requestTypeGET parDic:nil finish:^(NSData *data) {
    NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            
    self.modelArray = [HZWPictureModel creatPictureModel:dic];
            
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [self.tableView reloadData];
                    });
                NSLog(@"okkkkkkkkkk");
            
                } error:^(NSError *error) {
                    NSLog(@"请求失败");
                }];
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.aa = 30;
    // 调用网络请求方法
    [self craetPicture];
    // 注册
    [self.tableView registerClass:[HZWPictureTableViewCell class] forCellReuseIdentifier:@"three"];
    
    self.tableView .mj_footer =[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self requestFresh];
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //NSLog(@"%+++++ld",self.modelArray.count);
    return self.modelArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HZWPictureTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"three" forIndexPath:indexPath];
    
    HZWPictureModel * model = _modelArray[indexPath.row];
    
    [cell.aoturImage sd_setImageWithURL:[NSURL URLWithString:model.aotuImage] completed:nil];
    cell.aoturName.text= model.autoName;
    cell.passTime.text = model.passtime;
    [cell.jubaoButon addTarget:self action:@selector(jubaoButonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    // 调用返回内容的自定义高度
    [cell cellConfigureModel:model];
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
    

  
    return cell;
}
-(void)jubaoButonAction:(UIButton *)button
{
    UIAlertController * alert =[UIAlertController alertControllerWithTitle:@"提示" message:@"请输入您举报的内容" preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:
                      UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action)
    {
        UIAlertController * aler = [UIAlertController alertControllerWithTitle:@"提示" message:@"您举报的内容 已提交成功 我们会进一步的进行核实 谢谢" preferredStyle:UIAlertControllerStyleAlert];
        
        [aler addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        [self presentViewController:aler animated:NO completion:nil];
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        
    }];
    [self presentViewController:alert animated:NO completion:nil];
    
    
}
// 点击cell的事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HZWWebViewController * my = [[HZWWebViewController alloc]init];
    HZWPictureModel * model = _modelArray[indexPath.row];
    // 将数据传到webview界面
    my.shareWebstring  = model.share_url;

    [self.navc pushViewController:my animated:NO];


}
// 返回cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
       HZWPictureModel * model = _modelArray[indexPath.row];
    // 进行判断 如果 内容为空 将cell的高度为0
    if (model.text == nil)
    {
        return 0;
    }
    // 调用返回cell高度的方法
    
    return [HZWPictureTableViewCell cellHeghtForRow:model];
    
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
